//
//  YJDHud.m
//  sigleAPP
//
//  Created by SKL on 2017/11/24.
//  Copyright © 2017年 SKL. All rights reserved.
//

#import "YJDHud.h"
@interface YJDHud ()
@property (nonatomic, strong) UIActivityIndicatorView *hudView;
@property (nonatomic, assign) UIActivityIndicatorViewStyle style;
@end
@implementation YJDHud
- (instancetype)initWithHudStyle:(UIActivityIndicatorViewStyle)style
{
    self = [super init];
    if (self)
    {
        _style = style;
        self.bounds = CGRectMake(0, 0, 100, 100);
        self.layer.cornerRadius = 5.0;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor blackColor];
        self.alpha = 0.5;
        self.color = [YJDHelper shareHelp].color;
    }
    return self;
}

static YJDHud *_hud;
static dispatch_once_t YJDHudOnceToken;
+ (instancetype)yjd_hud
{
    if (!_hud)
    {
        dispatch_once(&YJDHudOnceToken,^{
            _hud = [[YJDHud alloc]initWithHudStyle:0];
        });
    }
    return _hud;
}

- (UIActivityIndicatorView *)hudView
{
    if (!_hudView)
    {
        _hudView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:_style];
        [self addSubview:_hudView];
        [_hudView mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    }
    return _hudView;
}

- (void)setupLayout
{
    NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:_hudView attribute:NSLayoutAttributeCenterX
                                                               relatedBy:NSLayoutRelationEqual toItem:self
                                                               attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    
    NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:_hudView attribute:NSLayoutAttributeCenterY
                                                               relatedBy:NSLayoutRelationEqual toItem:self
                                                               attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    //
    //    NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:_hudView attribute:NSLayoutAttributeWidth
    //                                                             relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil
    //                                                             attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:self.bounds.size.width];
    //
    //    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:_hudView attribute:NSLayoutAttributeHeight
    //                                                              relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil
    //                                                              attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:self.bounds.size.height];
    [self addConstraints:@[centerX,centerY]];
}

- (void)setColor:(UIColor *)color
{
    if (_color != color)
    {
        _color = color;
        self.hudView.color = color;
    }
}

- (void)setTimes:(CGFloat)times
{
    if (_times != times)
    {
        _times = times;
        
        if (self.hudView.bounds.size.width * times <= self.bounds.size.width )
        {
            self.hudView.transform = CGAffineTransformMakeScale(times, times);
        }
    }
}

+ (void)yjd_startHud
{
    dispatch_async(dispatch_get_main_queue(), ^{
        _hud = [YJDHud yjd_hud];
        _hud.hidden = NO;
        if (!_hud.superview)
        {
            UIWindow *window = [UIApplication sharedApplication].delegate.window;
            if (window)
            {
                [window addSubview:_hud];
                [_hud mas_makeConstraints:^(SK_MASConstraintMaker *make) {
                    make.center.equalTo(window);
                    make.size.mas_equalTo(_hud.bounds.size);
                }];
            }
        }
        
        
        [_hud.hudView startAnimating];
    });
}

+ (void)yjd_stopHud
{
    dispatch_async(dispatch_get_main_queue(), ^{
        _hud = [YJDHud yjd_hud];
        _hud.hidden = YES;
        [_hud.hudView stopAnimating];
        [_hud removeFromSuperview];
    });
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
